package com.etonedu.meals.dao;

import com.etonedu.meals.base.BaseApplication;
import com.etonedu.meals.bean.AttendanceDate;
import com.etonedu.meals.bean.CardLog;
import com.etonedu.meals.bean.MachineTimeBean;
import com.etonedu.meals.bean.MachineTimeBean_;
import com.etonedu.meals.bean.ScreenTimeBean;
import com.etonedu.meals.bean.ScreenTimeBean_;
import com.etonedu.meals.bean.StudentBean;
import com.etonedu.meals.bean.TimeList;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;

public class DaoUtils {

    private static BoxStore boxStore;

    public static BoxStore getBoxStore() {
        if (boxStore == null) {
            boxStore = ((BaseApplication)x.app()).get();
        }
        return boxStore;
    }

    public static Box<AttendanceDate> getBoxAttendanceDate() {
        BoxStore boxStore = getBoxStore();
        Box<AttendanceDate> box = boxStore.boxFor(AttendanceDate.class);
        return box;
    }

    public static Box<StudentBean> getBoxStudent() {
        BoxStore boxStore = getBoxStore();
        Box<StudentBean> box = boxStore.boxFor(StudentBean.class);
        return box;
    }

    public static Box<CardLog> getBoxCardLog() {
        BoxStore boxStore = getBoxStore();
        Box<CardLog> box = boxStore.boxFor(CardLog.class);
        return box;
    }

    public static Box<MachineTimeBean> getBoxMachineTimeBean() {
        BoxStore boxStore = getBoxStore();
        Box<MachineTimeBean> box = boxStore.boxFor(MachineTimeBean.class);
        return box;
    }

    public static Box<ScreenTimeBean> getBoxScreenTimeBean() {
        BoxStore boxStore = getBoxStore();
        Box<ScreenTimeBean> box = boxStore.boxFor(ScreenTimeBean.class);
        return box;
    }

    public static Box<TimeList> getBoxTimeList() {
        BoxStore boxStore = getBoxStore();
        Box<TimeList> box = boxStore.boxFor(TimeList.class);
        return box;
    }
    // 查询
    public static List<CardLog> queryCardLog() {
        List<CardLog> list = new ArrayList<>();
        try {
            BoxStore boxStore = getBoxStore();
            Box<CardLog> cardLogBox = boxStore.boxFor(CardLog.class);
            list = cardLogBox.query().build().find();
        } catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    //增加
    public static void insertCardLog(CardLog cardLog) {
        try {
            BoxStore boxStore = getBoxStore();
            Box<CardLog> cardLogBox = boxStore.boxFor(CardLog.class);
            cardLogBox.put(cardLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //增加list
    public static void insertCardLogList(List<CardLog> cardLogs) {
        try {
            BoxStore boxStore = getBoxStore();
            Box<CardLog> cardLogBox = boxStore.boxFor(CardLog.class);
            cardLogBox.put(cardLogs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 删除
    public static void deletCardLog(CardLog cardLog) {
        try {
            BoxStore boxStore = getBoxStore();
            Box<CardLog> cardLogBox = boxStore.boxFor(CardLog.class);
            cardLogBox.remove(cardLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void delletLtCurrTime4MachineTimeBean() {
        Box<MachineTimeBean> box = getBoxMachineTimeBean();
        List<MachineTimeBean> list = box.query().less(MachineTimeBean_.onTime,System.currentTimeMillis()).build().find();

        box.remove(list);
    }

    public static void delletScreenDaoLtCurrTime() {
        Box<ScreenTimeBean> box = getBoxScreenTimeBean();
        List<ScreenTimeBean> list = box.query().less(ScreenTimeBean_.endTime,System.currentTimeMillis()).build().find();

        box.remove(list);
    }
}
